package source.model;

import source.view.MainScreen;
import source.events.action.Action;
import source.model.type.*;
import source.model.skill.*;

public class TrapItem extends Item implements TriggeredItem {
	
	private int strength;
	
	public TrapItem(int strength) {
		super(ItemType.TRAP);
		this.strength = strength;
	}
	
	public TrapItem(){
		this(3);
	}

	public Action trigger() {
		
		final ObjectID thisItem = this.objectID();
		return new Action(){
			public ActionResult execute( DataHandler dh){
				GameObject go = dh.getCurrentSelection();
				if ( go.objectType() != GameObjectType.UNIT ) return ActionResult.IMPOSSIBLE;
				
				Unit unit = (Unit)go;
				
				Skill aSkill = unit.getSkill("remove traps");
				if (aSkill == null) unit.modHealth(-strength);
				MainScreen.writeToConsole("You " + (aSkill == null ? "hit" : "removed") +" a trap with " + strength + " strength!!");
				MainScreen.writeToConsole("Unit's Health = " + unit.currentHealth());
				GameObject me = dh.objectQuery(thisItem);
				me.invalidate();
				dh.returnObject(me);
				return ActionResult.SUCCESS;
			}
		};
	}

}
